﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2019-09-12
 * Time: 6:53
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace QStarter
{
	/// <summary>
	/// Description of NativeMethods.
	/// </summary>
	public class NativeMethods
	{
	    #region 内存回收
	    
	    [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
	    public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
	    
	    public static void ClearMemory()
	    {
	        GC.Collect();
	        GC.WaitForPendingFinalizers();
	        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
	        {
	            SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
	        }
	    }
	    
	    #endregion
		
        [DllImport("User32.dll")]
        public static extern int PrivateExtractIcons(
		    string lpszFile, //文件名可以是exe,dll,ico,cur,ani,bmp
		    int nIconIndex,  //从第几个图标开始获取
		    int cxIcon,      //获取图标的尺寸x
		    int cyIcon,      //获取图标的尺寸y
		    IntPtr[] phicon, //获取到的图标指针数组
		    int[] piconid,   //图标对应的资源编号
		    int nIcons,      //指定获取的图标数量，仅当文件类型为.exe 和 .dll时候可用
		    int flags        //标志，默认0就可以，具体可以看LoadImage函数
        );

        [DllImport("User32.dll")]
        public static extern bool DestroyIcon(
            IntPtr hIcon //要销毁的图标的句柄
        );
        
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,                //要定义热键的窗口的句柄
            int id,                     //定义热键ID（不能与其它ID重复）           
//          KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
			int modifiers,				
            Keys vk                     //定义热键的内容
        );
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                //要取消热键的窗口的句柄
            int id                      //要取消热键的ID
        );
        
  		[DllImport("shell32.dll")]
        public static extern IntPtr ShellExecute(
			IntPtr hwnd,
			string lpszOp,
			string lpszFile,
			string lpszParams,
			string lpszDir,
			int cmdShow
        );
        
		[DllImport("user32.dll", EntryPoint = "FindWindow")]  
		public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
		
		[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
		public static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
		
		[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
	}
}
